﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

/// <summary>
/// 
/// </summary>
namespace BloomsburyBanHang
{
    public class WSRequest
    {
        public string method { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string sid { get; set; }
        public int categoryId { get; set; }
        public int productId { get; set; }

        public WSRequest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        // Serialize an object into XML: http://www.dotnetfunda.com/articles/article98.aspx
        public static string ObjectToXML(object obj)
        {
            string retval = "";
            XmlDocument doc = new XmlDocument();
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            try
            {
                serializer.Serialize(stream, obj);
                stream.Position = 0;
                doc.Load(stream);
                retval = doc.InnerXml;
            }
            catch
            {
                retval = "";
            }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
            return retval;
        }

        // DeSerialize an object into XML: http://www.dotnetfunda.com/articles/article98.aspx
        public static bool XMLToObject<T>(ref T obj, string xmlRequest)
        {
            bool retval = true;
            StringReader read = new StringReader(xmlRequest);
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            XmlReader reader = new XmlTextReader(read);
            try
            {
                obj = (T)serializer.Deserialize(reader);
            }
            catch
            {
                retval = false;
            }
            finally
            {
                reader.Close();
                read.Close();
                read.Dispose();
            }
            return retval;
        }

        public  static string SerializeAnObject(object obj)
        {

            System.Xml.XmlDocument doc = new XmlDocument();

            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

            System.IO.MemoryStream stream = new System.IO.MemoryStream();

            try
            {

                serializer.Serialize(stream, obj);

                stream.Position = 0;

                doc.Load(stream);

                return doc.InnerXml;

            }

            catch
            {

                throw;

            }

            finally
            {

                stream.Close();

                stream.Dispose();

            }

        }
        public static string EditToBasicFormat(string s)
        {
            string[] banList = new string[1];
            string[] result = new string[1];
            string StringResult = "";
            string s1 = "xmlns=\"http://tempuri.org/\"";
            banList[0] = s1;
            result = s.Split(banList, System.StringSplitOptions.RemoveEmptyEntries);
            foreach (string t in result)
            {
                StringResult += t;
            }
            return StringResult;
        }
    }
}